﻿using System;
using System.Collections.Generic;
using System.Text;
using NDde.Client;
using System.Threading;

namespace BasicMachine
{
    public partial class Form1
    {
        private DdeClient dclient;
        public int m_IndexValue;
        public int m_OpeningValue = 0;
        public bool m_sent = false;
        public bool m_failSend = false;
        public bool m_Alive = true;

        private void CheckLive()
        {
            TimeSpan endTime = new TimeSpan(13, 45, 0);
            TimeSpan startTime = new TimeSpan(8, 45, 0);

            while (true)
            {
                if(DateTime.Now.TimeOfDay >= endTime || DateTime.Now.TimeOfDay <= startTime)
                    m_Alive = true;
                else if (m_Alive)
                    m_Alive = false;
                else
                {
                    TimeSpan exchange = new TimeSpan(13, 26, 00);

                    if (DateTime.Now.Date.DayOfWeek == DayOfWeek.Wednesday && DateTime.Now.TimeOfDay > exchange)
                        m_sent = true;
                    else if (!m_sent && DateTime.Now.Date.DayOfWeek!= DayOfWeek.Saturday && DateTime.Now.Date.DayOfWeek != DayOfWeek.Sunday)
                        SendAert("DDE SERVER DISCONNECTED");

                    m_sent = true;
                }
                Thread.Sleep(60000);
            }
        }


        private void StartReceive()
        {
            bool excepBreak = false;

            string ddeService = "FUTURETXF" + ReturnCharMonth(lab_Month.Text);
            dclient = new DdeClient("DDEEXCEL", ddeService, this);
            //dclient = new DdeClient("myapp", "myservice", this);
            dclient.Advise += new EventHandler<DdeAdviseEventArgs>(dclient_Advise);
            dclient.Disconnected += new EventHandler<DdeDisconnectedEventArgs>(dclient_Disconnected);

            Thread.Sleep(500);

            string result;
            try
            {
                dclient.Connect();
                dclient.StartAdvise("市價", 1, true, 60000);

                result = "DDE Current Month Connection Successful";

                SendEmailAlert(result);
            }
            catch (Exception ex)
            {
                lab_Index.ForeColor = System.Drawing.Color.Red;
                lab_Index.Text = "DDE Current MonthConnection Fail";
                result = "DDE Current Month Connection Fail";

                excepBreak = true;

                Thread.Sleep(10000);
                Thread ddeOps = new Thread(new ThreadStart(StartReceive));
                ddeOps.Start();
            }
            if (excepBreak && !m_failSend)
            {
                SendEmailAlert(result);
                m_failSend = true;
            }
        }

        private void SendEmailAlert(string msg)
        {
            AlertMail.MailTo = m_SystemMailList;
            AlertMail.MailSubject = msg;
            AlertMail.MailBody = DateTime.Now.TimeOfDay.ToString() + AlertMail.MailSubject;
            AlertMail.Send();
        }

        private void SendAert(string msg)
        {
            SMSender.sendMessage("DDE Server 斷線", "DDE Server 斷線!! ", "", "0938920062");

            AlertMail.MailTo = m_SystemMailList;
            AlertMail.MailSubject = msg;
            AlertMail.MailBody = DateTime.Now.TimeOfDay.ToString() + AlertMail.MailSubject;
            AlertMail.Send();
        }

        void dclient_Disconnected(object sender, DdeDisconnectedEventArgs e)
        {
            if (!m_sent)
            {
                SMSender.sendMessage("DDE Server 斷線", "DDE Server 斷線!! ", "", "0938920062");
                m_sent = true;
            }
            
            AlertMail.MailTo = m_SystemMailList;

            AlertMail.MailSubject = "DDE Server Disconnect !!";
            AlertMail.MailBody = DateTime.Now.TimeOfDay.ToString() + AlertMail.MailSubject;
            AlertMail.Send();

            lab_Index.Text = "DDE SERVER DISCONNECTED";
            Thread.Sleep(10000);
            Thread ddeOps = new Thread(new ThreadStart(StartReceive));
            ddeOps.Start();
        }

        void dclient_Advise(object sender, DdeAdviseEventArgs e)
        {
            lab_Index.ForeColor = System.Drawing.Color.MediumBlue;
            m_IndexValue = Convert.ToInt32(e.Text);
            lab_Index.Text = e.Text;

            m_sent = false;
            m_Alive = true;
        }
    }
}
